package com.zdsoft.blp.zongbuju.shouye.wuye.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.shouye.wuye.adapter.WYAdapter;
import com.zdsoft.blp.zongbuju.shouye.wuye.entity.WuYe;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/30.
 */
public class WuYeActivity extends BaseActivity implements NetCallback{
    private FanHuiView back;
    private ListView lv;
    private List<WuYe>list;
    private  WuYe wuYe;

    @Override
    protected void init() {
        setContentView(R.layout.wuye);
        back=find(R.id.back);
        lv=find(R.id.lv);
        back.getTv_title().setText("物业");

        Net net=new Net();
        net.url= API.BMINFO_URL;
        net.nameSpace=API.BMINFO_NAMESPACE;
        net.methodName=API.COLUMNCHILDLIST;
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String communityCode = sp.getString("communityCode", "00001");
        Log.e("co",communityCode);
        Map<String,Object> map=new HashMap<>();
        map.put("communityCode",communityCode);
        map.put("columnCode","wy");
        net.parms=map;
        getServer(net,this);



        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               String title= list.get(position).getTitle().toString().trim();
                Log.e("title",title);
                if("通知公告".equals(title)){
                    Intent intent=new Intent(WuYeActivity.this,WYTongZhiActivity.class);
                    startActivity(intent);
                }
                if("问题留言".equals(title)){
                    Intent intent=new Intent(WuYeActivity.this,WYLiuYanActivity.class);
                    startActivity(intent);
                }
                if ("缴费大厅".equals(title)){
                    Intent intent=new Intent(WuYeActivity.this,WYJiaoFeiActivity.class);
                    intent.putExtra("id",id);
                    startActivity(intent);
                }
            }
        });
  }


    @Override
    public void onResponse(boolean flag, int code, String res) {

        if (flag){
            list=jiexi(res);
            WYAdapter adapter=new WYAdapter(getApplicationContext(),list);
             lv.setAdapter(adapter);
        }
    }

    private List<WuYe> jiexi(String res) {
        list = new ArrayList<>();
        try {
            JSONObject object=new JSONObject(res);
            boolean result=object.getBoolean("result");
            JSONArray array=object.getJSONArray("value");
            for(int i=0;i<array.length();i++){
                JSONObject object2=array.getJSONObject(i);
                String content=object2.getString("desc");
                String title=object2.getString("name");
                String imgPath=object2.getString("bmColumnImgPath");
                String id=object2.getString("id");
                wuYe=new WuYe(id,title,content,imgPath);
                list.add(wuYe);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }
}
